package alfred.proctice.com.recycleview;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

/**
 * Created by self on 2017/11/8.
 */

public class GlideUtil {


    public static void loadCircle(Context context, String path, ImageView img) {
        loadCircle(context, path, img, 0);
    }

    public static void loadCircle(final Context context, String path, final ImageView img, int res) {
        Glide.with(context).load(path).asBitmap().fitCenter().placeholder(R.drawable.loading_picture).error(res == 0 ? R.drawable.register_header : res).into(new BitmapImageViewTarget(img) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                img.setImageDrawable(circularBitmapDrawable);
            }
        });
    }

    public static void loadImg(Context context, String path, final ImageView img) {
        loadImg(context, path, img, 0);
    }

    public static void loadImg(Context context, String path, final ImageView img, int res) {
        Glide.with(context).load(path).thumbnail(0.1f).placeholder(R.drawable.loading_picture).error(res == 0 ? R.drawable.loading_picture : res)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(img);
    }

    public static void loadFitCenter(Context context, String path, final ImageView img) {
        loadFitCenter(context, path, img);
    }

    public static void loadFitCenter(Context context, String path, final ImageView img, int res) {
        Glide.with(context).load(path).thumbnail(0.1f).placeholder(R.drawable.loading_picture).error(res == 0 ? R.drawable.loading_picture : res)
                .fitCenter().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(img);

    }

    public static void loadCenterCrop(Context context, String path, final ImageView img) {
        loadCenterCrop(context, path, img, 0);

    }

    public static void loadCenterCrop(Context context, String path, final ImageView img, int res) {
        Glide.with(context).load(path).thumbnail(0.1f).placeholder(R.drawable.loading_picture).error(res == 0 ? R.drawable.loading_picture : res)
                .centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(img);

    }

    /**
     * 加载原图片 不进行缩略
     * @param context
     * @param path
     * @param img
     * @param res
     */
    public static void loadFullImg(Context context, String path, final ImageView img, int res) {
        Glide.with(context).load(path).placeholder(R.drawable.loading_picture).error(res == 0 ? R.drawable.loading_picture : res)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(img);
    }
}
